package com.servlet;

import com.beans.AdminInfo;
import com.beans.GoodsInfo;
import com.beans.PageInfo;
import com.dao.GoodsDao;
import com.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/GoodsServlet.do")
@MultipartConfig //文件上传需要加上
public class GoodsServlet extends HttpServlet {
    GoodsDao goodsDao = new GoodsDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");
        if("checkGoodsName".equals(flag)){
            checkGoodsName(request,response);
        }
        else if("add".equals(flag)){
            add(request,response);
        }
        else if("manage".equals(flag)){
            manage(request,response);
        }
    }
    //商品维护
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = 6;
        int rowCount = goodsDao.getGoodsCount();
        int pageIndex = 1;
        String pageIndexStr = request.getParameter("pageIndex");
        if(pageIndexStr!=null){
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        PageInfo page = PageUtil.getPageInfo(pageSize,rowCount,pageIndex);

        List<GoodsInfo> goodsList = goodsDao.getGoodsList(page);
        request.setAttribute("goodsList",goodsList);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/goods/goods_manage.jsp").forward(request,response);
    }

    //添加商品
    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String goodsName = request.getParameter("goodsName");
        String unit = request.getParameter("unit");
        double price =Double.parseDouble( request.getParameter("price"));
        String productor = request.getParameter("productor");
        String des = request.getParameter("des");

        Integer bigCateId = Integer.parseInt(request.getParameter("select1"));
        Integer smallCateId = Integer.parseInt(request.getParameter("select2"));
        //获取图片数据
        Part part = request.getPart("picture");
        InputStream in = part.getInputStream();
        byte[] buff = new byte[in.available()];
        in.read(buff);

        GoodsInfo goods = new GoodsInfo();
        goods.setGoodsName(goodsName);
        goods.setUnit(unit);
        goods.setPrice(price);
        goods.setProductor(productor);
        goods.setDes(des);
        goods.setPictureData(buff);

        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        //用于前台回显图片
        int goodsId = goodsDao.addGoods(goods);
        goods.setId(goodsId);
        request.setAttribute("goods",goods);
        request.setAttribute("msg","添加成功");
        request.getRequestDispatcher("/goods/goods_add.jsp").forward(request,response);
    }

    //添加商品时检查商品名是否重复
    private void checkGoodsName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsName = request.getParameter("goodsName");
        GoodsInfo goods=goodsDao.checkGoodsName(goodsName);
        if(goods!=null){
            response.getWriter().print("-1");
        }
    }
}
